<template>
  <div class="app-container">
    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
        >新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
        >删除</el-button>
      </el-col>
    </el-row>

    <el-table v-loading="loading" :data="list" @selection-change="handleSelectionChange" max-height="600" style="width: 100%">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="搜索热词" align="center" prop="value" />
      <el-table-column label="排序" align="center" prop="score" />
      <el-table-column fixed="right" label="操作" align="center" width="180">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
          >修改</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
          >删除</el-button>
        </template>
      </el-table-column>
    </el-table>

    <el-dialog :visible.sync="editOpen" title="填写发货信息">
      <el-form
        ref="editForm"
        :model="editForm"
        :rules="editRules"
        status-icon
        label-position="left"
        label-width="100px"
        style="width: 400px; margin-left:50px;"
      >
        <el-form-item label="热词" prop="value">
          <el-input v-model="editForm.value" :disabled="editForm.type == 2"/>
        </el-form-item>
        <el-form-item label="排序" prop="score">
          <el-input-number v-model="editForm.score" :min="1" ></el-input-number>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="editOpen = false">取消</el-button>
        <el-button type="primary" @click="handleSubmit">确定</el-button>
      </div>
    </el-dialog>

  </div>
</template>

<script>
  import { listHotWord, saveHotWord, delHotWord } from "@/api/operate/operate";

  export default {
    name: "HotWordList",
    data() {
      return {
        // 遮罩层
        loading: true,
        // 选中数组
        values: [],
        // 非单个禁用
        single: true,
        // 非多个禁用
        multiple: true,
        // 表格数据
        list: [],
        editOpen: false,
        editForm: {},
        editRules: {
          value: [{ required: true, trigger: "blur", message: "热词不能为空" }],
          score: [{ required: true, trigger: "blur", message: "排序值不能为空" }]
        }
      };
    },
    created() {
      this.getList();
    },
    methods: {
      /** 查询商品类型列表 */
      getList() {
        this.loading = true;
        listHotWord().then(response => {
            this.list = response.data;
            this.loading = false;
          }
        );
      },
      // 多选框选中数据
      handleSelectionChange(selection) {
        this.values = selection.map(item => item.value)
        this.single = selection.length!=1
        this.multiple = !selection.length
      },
      /** 删除按钮操作 */
      handleDelete(row) {
        const values = row.value ? [row.value] : this.values;
        this.$confirm('是否删除：' + values + '?', "警告", {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning"
        }).then(function() {
          return delHotWord(values);
        }).then(() => {
          this.getList();
          this.msgSuccess("删除成功");
        }).catch(function() {});
      },
      /** 新增按钮操作 */
      handleAdd() {
        this.resetForm("editForm");
        this.editForm = {
          type: 1
        }
        this.editOpen = true;
      },
      /** 修改按钮操作 */
      handleUpdate(row) {
        this.resetForm("editForm");
        this.editForm = {
          type: 2,
          value: row.value,
          score: row.score
        }
        this.editOpen = true;
      },
      handleSubmit() {
        this.$refs.editForm.validate(valid => {
          if (valid) {
            saveHotWord(this.editForm).then(res => {
              this.editOpen = false;
              this.getList();
            })
          } else {
            return false;
          }
        });
      }
    }
  };
</script>
